package com.waitingresult.common.base.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

import static com.waitingresult.common.constant.Constants.ResultStatusCode.*;

/**
 * @Author: WangShuai
 * @Date: 2021-09-07 18:26:13
 * @Desc: 统一结果集返回对象
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 3728877563912075885L;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Integer code;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String msg;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String token;

    public Result(T data) {
        this.data = data;
    }

    public Result(String msg, T data) {
        this.msg = msg;
        this.data = data;
    }

    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Result(Integer code, String msg, T data, String token) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.token = token;
    }

    /**
     * 成功的构造
     */
    public static <T> Result<T> success(String msg) {
        return new Result<T>(SUCCESS, msg, null);
    }

    public static <T> Result<T> success(String msg, T data) {
        return new Result<T>(SUCCESS, msg, data);
    }

    public static <T> Result<T> success(String msg, T data, String token) {
        return new Result<T>(SUCCESS, msg, data, token);
    }

    /**
     * 失败的构造
     */
    public static <T> Result<T> error(String msg) {
        return new Result<T>(ERROR, msg, null);
    }

    public static <T> Result<T> error(String msg, T data) {
        return new Result<T>(ERROR, msg, data);
    }

    public static <T> Result<T> error(String msg, T data, String token) {
        return new Result<T>(ERROR, msg, data, token);
    }


    public static <T> Result<T> needLogin(String msg) {
        return new Result<T>(NEED_LOGIN, msg, null);
    }

    public static <T> Result<T> noPermission(String msg) {
        return new Result<T>(NO_PERMISSION, msg, null);
    }

    @Override
    public String toString() {
        return "RestResult{" + "code=" + code + ", msg='" + msg + '\'' + ", data=" + data + '}';
    }
}
